package com.dog.modules.message.service.impl;

import cn.hutool.db.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dog.framework.core.constant.Constant;
import com.dog.modules.message.cache.SmsPlatformCache;
import com.dog.modules.message.convert.SmsPlatformConvert;
import com.dog.modules.message.domain.entity.SmsPlatform;
import com.dog.modules.message.mapper.SmsPlatformMapper;
import com.dog.modules.message.domain.query.SmsPlatformQuery;
import com.dog.modules.message.domain.vo.SmsPlatformVO;
import com.dog.modules.message.service.SmsPlatformService;
import com.dog.modules.message.sms.config.SmsConfig;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liuxk
 * @description 针对表【sms_platform(短信平台)】的数据库操作Service实现
 * @createDate 2024-01-24 17:18:18
 */
@Service
@AllArgsConstructor
public class SmsPlatformServiceImpl extends ServiceImpl<SmsPlatformMapper, SmsPlatform> implements SmsPlatformService {

    private final SmsPlatformCache smsPlatformCache;

    @Override
    public PageResult<SmsPlatformVO> page(SmsPlatformQuery query) {
        return null;
    }

    @Override
    public List<SmsConfig> listByEnable() {

        // 从缓存读取
        List<SmsConfig> cacheList = smsPlatformCache.list();

        // 如果缓存没有，则从DB读取，然后保存到缓存里
        if(cacheList == null) {
            List<SmsPlatform> list = this.list(new LambdaQueryWrapper<SmsPlatform>().in(SmsPlatform::getStatus, Constant.ENABLE));

            cacheList = SmsPlatformConvert.INSTANCE.convertList2(list);
            smsPlatformCache.save(cacheList);
        }

        return cacheList;
    }

    @Override
    public void save(SmsPlatformVO vo) {
        SmsPlatform entity = SmsPlatformConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);

        smsPlatformCache.delete();
    }

    @Override
    public void update(SmsPlatformVO vo) {
        SmsPlatform entity = SmsPlatformConvert.INSTANCE.convert(vo);

        updateById(entity);

        smsPlatformCache.delete();
    }

    @Override
    public void delete(List<Long> idList) {
        removeByIds(idList);

        smsPlatformCache.delete();
    }
}




